package com.itboye.ihomebank.activity.finance;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.activity.finance.fragment.SanBiaoFragemnt;
import com.itboye.ihomebank.activity.finance.fragment.YouXuanFragemnt;
import com.itboye.ihomebank.activity.finance.fragment.ZhuanRangFragemnt;

import java.util.ArrayList;
import java.util.List;

/**
 * 投资
 */
public class TouZiActivity extends BaseOtherActivity {
    ImageView close_icon;
    TextView touzi_youxuan,touzi_sanbiao,touzi_zhuanrang;
    ViewPager viewPager;

    List<Fragment> fragments = new ArrayList<>();
    @Override
    protected int layout_id() {
        return R.layout.activity_tou_zi;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        YouXuanFragemnt youxuan = new YouXuanFragemnt();
        SanBiaoFragemnt sanbiao = new SanBiaoFragemnt();
        ZhuanRangFragemnt zhuanrang=new ZhuanRangFragemnt();
        fragments.add(youxuan);
        fragments.add(sanbiao);
        fragments.add(zhuanrang);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setCurrentItem(0);
        changeTextBG(0);

        touzi_sanbiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        touzi_zhuanrang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        touzi_youxuan.setOnClickListener(this);
        viewPager.setOnPageChangeListener(new myPagerListener());

        // 短信登录发送过来的广播
        IntentFilter filter1 = new IntentFilter("goumaiend");
        registerReceiver(endReceiver, filter1);
    }
    BroadcastReceiver endReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        };
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(endReceiver);
    }
    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent=null;
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;
            case R.id.touzi_youxuan:
                viewPager.setCurrentItem(0);
                break;
            case R.id.touzi_sanbiao:
                viewPager.setCurrentItem(1);
                break;
            case R.id.touzi_zhuanrang:
                viewPager.setCurrentItem(2);
                break;
        }
    }
    private void changeTextBG(int position) {
        switch (position) {
            case 0:
                touzi_youxuan.setBackgroundColor(getResources().getColor(R.color.blue_6));
                touzi_youxuan.setTextColor(getResources().getColor(R.color.blue_5));
                touzi_sanbiao.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_sanbiao.setTextColor(getResources().getColor(R.color.white));
                touzi_zhuanrang.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_zhuanrang.setTextColor(getResources().getColor(R.color.white));
                break;
            case 1:
                touzi_youxuan.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_youxuan.setTextColor(getResources().getColor(R.color.white));
                touzi_sanbiao.setBackgroundColor(getResources().getColor(R.color.blue_6));
                touzi_sanbiao.setTextColor(getResources().getColor(R.color.blue_5));
                touzi_zhuanrang.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_zhuanrang.setTextColor(getResources().getColor(R.color.white));
                break;
            case 2:
                touzi_youxuan.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_youxuan.setTextColor(getResources().getColor(R.color.white));
                touzi_sanbiao.setBackgroundColor(getResources().getColor(R.color.blue_5));
                touzi_sanbiao.setTextColor(getResources().getColor(R.color.white));
                touzi_zhuanrang.setBackgroundColor(getResources().getColor(R.color.blue_6));
                touzi_zhuanrang.setTextColor(getResources().getColor(R.color.blue_5));
                break;
        }
    }
    class MyPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragments;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
    class myPagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.setCurrentItem(position);
            changeTextBG(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
